<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">

    <div th:replace="~{commons/script::script}"></div>

</head>

<body>
<div id="tools" style="margin-bottom: 20px">
    <table>
        <tr>
            <form action="/customer/searchs">
                <td>
                    <i class="iconfont"></i>
                    <select name="batchOperate" style="width:100px;height:25px;" id="batchOperate">
                        <option value="-1">批量操作</option>
                        <option value="0">批量删除</option>
                    </select>
                </td>
                <td>
                    <select name="filterSearch" style="width:100px;height:25px;" id="batchOperate1">
                        <option value="-1">--请选择筛选条件--</option>
                        <option value="0">按客户名查询</option>
                        <option value="1">按地址查询</option>
                        <option value="2">按创建时间起查询</option>
                        <option value="3">按行业查询</option>
                    </select>
                    <select name="contain" style="width:60px;height:25px;">
                        <option value="-1">--包含--</option>
                    </select>
                </td>
                <td class="text-content">
                    <input name="search" type="text" placeholder="请输入查询关键字">
                    <button type="submit" id="search" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true">搜索</button>
                </td>
            </form>

            <td>
                <span id="chat" class="easyui-linkbutton" data-options="iconCls:'icon-cut',plain:true">发送短信</span>
                <span id="email" class="easyui-linkbutton" data-options="iconCls:'icon-redo',plain:true">发送邮件</span>
            </td>

            <td>
                <span onclick="return add('/customer/cus')" class="easyui-linkbutton"
                      data-options="iconCls:'icon-add',plain:true">新建客户</span>
                <span id="tool" class="easyui-linkbutton" data-options="iconCls:'icon-cog',plain:true">客户工具</span>
            </td>
        </tr>
    </table>
</div>
<div id="customerLs" class="easyui-datagrid" style="width:100%;"></div>

</body>

<script type="text/javascript">
    $(function () {
        //分页展示客户列表
        $("#customerLs").datagrid({
            url: "/customer/toLis",
            checkOnSelect: false,
            idField: "customerId",
            pagination: true,
            sortName: "customerId",
            columns: [
                [
                    {field: "选择", checkbox: true},
                    {field: "id", hidden: true},
                    {field: "name", title: "客户名称",width:80},
                    {field: "industry", title: "客户行业",width:80},
                    {field: "rating", title: "评分",width:80},
                    {
                        field: "address", title: "客户联系地址", formatter: function (value) {
                            return "<span title='" + value + "'>" + value + "</span>"
                        },width:80
                    },
                    {field: "createUser", title: "创建人",width:80},
                    {
                        field: "createTime", title: "创建时间", width: 50, formatter: function (value) {
                            return "<span title='" + value + "'>" + value + "</span>"
                        },width:80
                    },
                    {
                        field: "updateTime", title: "更新时间", width: 100, formatter: function (value) {
                            if (value) {
                                return "<span title='" + value + "'>" + value + "</span>";
                            }
                            return "";
                        }
                    },
                    {
                        field: "operation", title: "操作", width: 120, formatter: function (value, rowData, index) {
                            return "<a href='javascript:void(0);' onclick=\"return see(\'/customer/sho\',\'" + rowData.id + "\')\">查看</a>" +
                                " <a href='javascript:void(0);' onclick=\"return modify(\'/customer/cu\',\'" + rowData.id + "\')\">编辑</a>"+
                                " <a href='javascript:void(0);' onclick=\"return modify(\'/customer/cu\',\'" + rowData.id + "\')\">分配</a>"+
                                " <a href='javascript:void(0);' onclick=\"return modify(\'/customer/cu\',\'" + rowData.id + "\')\">领取</a>"
                        }
                    }

                ]
            ],
            loadFilter: function (data) {
                return {total: data.data.totalRows, rows: data.data.result}
            }

        })
    })

    //批量删除
    function removes(url){
        var row=$("#customerLs").datagrid("getSelections");
        if (!row.length) {
            $.messager.alert("警告","请选择要删除的数据");
            return;
        }

        $.messager.confirm("警告","数据删除后无法恢复，是否确认删除",function(b) {
            if(b){
                for(let i of row){
                    $.get(url,{id:i.id},function (data) {
                        // $.messager.alert("警告",data.msg);
                        return;
                    })
                }
                location.href = "/customer/toCustomers";
            }
        });
    }
    //关注客户事件
    function follow(id, index) {
        $.post("manage/customer/CustomerAction_follow.action", {
            "customerId": id
        }, function (data) {
            if (data && data.success) {
                $("#customerLs").datagrid("reload", {
                    "index": index,
                    "row": {
                        "follow": "<img src='/static/easyui/themes/icons/star.png'/>"
                    }
                })
            }
        }, "json")

    }

    //取消关注客户
    function unfollow(id, index) {
        $.post("manage/customer/CustomerAction_unfollow.action", {
            "customerId": id,
        }, function (data) {
            if (data && data.success) {
                $("#customerLs").datagrid("reload", {
                    "index": index,
                    "row": {
                        "follow": "<img src='/static/easyui/themes/icons/stark.png'/>"
                    }
                })
            }
        }, "json")
    }

    //查询全部客户
    function whole(url) {
        /*$("#customerLs").datagrid({
            "url": url
        })*/
        location.href = url;
    }

    //查询我的客户
    function myCustomer(url) {
        /*$("#customerLs").datagrid({
            "url": url
        })*/
        location.href = url;
    }

    //查询当前员工下属创建的客户
    function subCustomer(url) {
        $("#customerLs").datagrid({
            "url": url
        })
    }

    //查询今天创建的客户
    function todayCreate(url) {
        /*$("#customerLs").datagrid({
            "url": url + "?type=day"
        })*/
        location.href = url + "?type=day";
    }

    //查询周创建的客户
    function weekCreate(url) {
        /*$("#customerLs").datagrid({
            "url": url + "?type=week"
        })*/
        location.href = url + "?type=week";
    }

    //查询月创建的客户
    function monthCreate(url) {
       /*$("#customerLs").datagrid({
            "url": url
        })*/
        location.href = url + "?type=month";
    }

    //查询当前用户关注的客户
    function followed(url) {
        $("#customerLs").datagrid({
            "url": url
        })
    }

    //页面添加客户
    function add(url) {
        parent.parent.openTopWindow({
            "url": url,
            "width": 800,
            "height": 700,
            "title": "新建客户",
            close: function () {
                $("#customerLs").datagrid("reload");
            }
        })
    }

    //修改客户
    function modify(url, id) {
        parent.parent.openTopWindow({
            "url": url + "?id=" + id,
            "width": 800,
            "height": 700,
            "title": "新建客户",
            close: function () {
                $("#customerLs").datagrid("reload");
            }
        })
    }

    //查看客户
    function see(url, id) {
        parent.parent.openTopWindow({
            "url": url + "?id=" + id,
            "width": 800,
            "height": 700,
            "title": "客户信息",
            close: function () {
                $("#customerLs").datagrid("reload");
            }
        })
    }
</script>
<script type="text/javascript">
    $(function () {
        //邮件发送
        $("#email").on("click", function (e) {
            var rows = $("#customerLs").datagrid("getSelections");
            if (!rows || rows.length == 0) {
                $.messager.alert("警告", "请选中要发送邮件的客户");
                return;
            }
            if(rows.length>1){
                $.messager.alert("警告","请选择单条数据进行发送邮件");
                return false;
            }
            var userId = rows[0].id;

            location.href = "/customer/emails?id=" + userId;
        })

        //短信发送
        $("#chat").on("click", function (e) {
            var rows = $("#customerLs").datagrid("getSelections");
            if (!rows || rows.length == 0) {
                $.messager.alert("警告", "请选中要发送短信的客户");
                return;
            }
            if(rows.length>1){
                $.messager.alert("警告","请选择单条数据进行短信发送");
                return false;
            }
            var userId = rows[0].id;

            location.href = "/customer/chat?id=" + userId;
        })

        //批量删除
        $("#batchOperate").bind("change",function () {
            var value = $(this).val();
            if (value == 0) {
                removes('/customer/toDel')
            }
        })
    })
</script>
</html>
